#include "bitmap.h"
#include <limits.h>

static int  get  (unsigned char  byte, int pos);
static void set  (unsigned char *byte, int pos);
static void reset(unsigned char *byte, int pos);

/* CAREFUL WITH pos AND BITMAP SIZE! */

int bitmapGet(unsigned char *bitmap, int pos) {
/* gets the value of the bit at pos */
	return get(bitmap[pos/CHAR_BIT], pos%CHAR_BIT);
}

void bitmapSet(unsigned char *bitmap, int pos) {
/* sets bit at pos to 1 */
    set(&bitmap[pos/CHAR_BIT], pos%CHAR_BIT);
}

void bitmapReset(unsigned char *bitmap, int pos) {
/* sets bit at pos to 0 */
    reset(&bitmap[pos/CHAR_BIT], pos%CHAR_BIT);
}

int bitmapSearch(unsigned char *bitmap, int size, int n, int start) {
/* Finds the first n value in bitmap after start */
/* size is the Bitmap size in bits */
	int i;
	for(i = start; i < size; i++)
  		if(bitmapGet(bitmap,i) == n)
      		return i;
	return BITMAP_NOTFOUND;
}

static int get(unsigned char byte, int pos) {
/* pos is a value between [0, CHAR_BIT) */
	return (byte >> pos) & 1;
}

static void set(unsigned char *byte, int pos) {
/* pos is a value between [0, CHAR_BIT) */
/* sets bit to 1 */
	*byte |= 1 << pos;
}

static void reset(unsigned char *byte, int pos) {
/* pos is a value between [0, CHAR_BIT) */
/* sets bit to 0 */
	*byte &= ~(1 << pos);
}

/*
int main(void) {
	unsigned char map[3] = {0};
	int i;
	
	for(i = 0; i < 24; i++)
	    bitmapSet(map, i);
	for(i = 0; i < 24; i++)
	    printf("%d ", bitmapGet(map, i));
	putchar('\n');
	for(i = 0; i < 24; i++)
	    bitmapReset(map, i);
	for(i = 0; i < 24; i++)
	    printf("%d ", bitmapGet(map, i));
	    
	bitmapSet(map, 0);
	bitmapSet(map, 22);
	printf("%d ", bitmapSearch(map, 24, 1, 26));
}*/

